package cn.com.cifi.mars.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

import static cn.com.cifi.mars.util.NumberUtils.isEmpty;
import static cn.com.cifi.mars.util.NumberUtils.numAdd;
import static java.math.MathContext.UNLIMITED;

public class BigDecimalUtils {

    /**
     * 方法描述：将BigDecimal除以Integer
     * @param a 被除数
     * @param b 除数
     * 创建人：jyx
     * 创建时间：2017年3月15日 下午3:13:03
     */
    public static BigDecimal divInteger(BigDecimal a, Integer b,int scale) {
        if(a == null || b == null){
            return BigDecimal.ZERO;
        }

        BigDecimal v1 = a;
        BigDecimal v2 = new BigDecimal(b.toString());

        if(v1.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.ZERO;
        }
        if(v2.compareTo(BigDecimal.ZERO)==0){
            return BigDecimal.ZERO;
        }

        return v1.divide(v2, scale, RoundingMode.HALF_UP);
    }

    /**
     * 方法描述：BigDecimal相除
     * @param a 被除数
     * @param b 除数
     * @param scale 保留小数位数
     * 创建人：jyx
     * 创建时间：2017年3月15日 下午3:13:03
     */
    public static BigDecimal divBigDecimal(BigDecimal a, BigDecimal b,int scale) {
        if(a == null || b == null){
            return BigDecimal.ZERO;
        }

        if(a.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.ZERO;
        }
        if(b.compareTo(BigDecimal.ZERO)==0){
            return BigDecimal.ZERO;
        }

        BigDecimal doubleValue = a.divide(b, scale, RoundingMode.HALF_UP);
        return doubleValue;
    }


    public static String toString(BigDecimal a,int scale){
        if(null==a){
            return "0";
        }
        return a.setScale(scale,RoundingMode.HALF_UP).toString();
    }

    /**
     * 计算百分率
     */
    public static String calculatedPercentage(BigDecimal a, BigDecimal b){
        if(a == null || b == null){
            return "0.0%";
        }

        if(a.compareTo(BigDecimal.ZERO) == 0){
            return "0.0%";
        }
        if(b.compareTo(BigDecimal.ZERO)==0){
            return "0.0%";
        }
        String text = a.divide(b, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100L)).setScale(1, RoundingMode.HALF_UP).toString();
        return NumberUtils.fmtPriceMicrometer(text,1)+"%";
    }

    /**
     * 计算百分率
     */
    public static String calculatedPercentageA(BigDecimal a, BigDecimal b){
        if(a == null || b == null){
            return "0.0%";
        }

        if(a.compareTo(BigDecimal.ZERO) == 0){
            return "0.0%";
        }
        if(b.compareTo(BigDecimal.ZERO)==0){
            return "0.0%";
        }
        String text = a.divide(b, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100L)).setScale(1, RoundingMode.HALF_UP).toString();
        return text + "%";
    }

    /**
     *  BigDecimal*BigDecimal
     */
    public static BigDecimal multiplyBigDecimal(BigDecimal money, BigDecimal price, int scale) {
        if(money == null || price == null){
            return BigDecimal.ZERO;
        }
        if(price.compareTo(BigDecimal.ZERO)==0 || money.compareTo(BigDecimal.ZERO)==0){
            return BigDecimal.ZERO;
        }
        return money.multiply(price).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 房源+车位
     * @param house
     * @param parking
     * @param type(num:套数相加,area:面积相加,amount:金额相加)
     * @return
     */
    public static String houseAddParking(BigDecimal house,BigDecimal parking,String type){

        if(type.equals("area")){
            return house.setScale(4,BigDecimal.ROUND_HALF_UP).toString();
        }else if(type.equals("num")){
            return house.setScale(4,BigDecimal.ROUND_HALF_UP).toString() + " / "
                    + parking.setScale(4,BigDecimal.ROUND_HALF_UP).toString();
        }else if(type.equals("amount")){
            return house.add(parking).setScale(4,BigDecimal.ROUND_HALF_UP) + " / "
                    + house.setScale(4,BigDecimal.ROUND_HALF_UP).toString() + " / "
                    + parking.setScale(4,BigDecimal.ROUND_HALF_UP).toString();
        }
        return null;
    }


}
